package com.wust.smartcity.autotask.server.common.aspect;


import com.alibaba.fastjson.JSONObject;
import com.wust.smartcity.autotask.server.core.mq.producer.Producer4routingKey;
import com.wust.smartcity.common.OperationLogAspectBase;
import com.wust.smartcity.common.entity.bl.BlOperationLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * Created by wust on 2019/5/27.
 */
@Aspect
@Order(1)
@Component
public class OperationLogAspect extends OperationLogAspectBase {
    @Autowired
    private Producer4routingKey producer4routingKey;

    @Autowired
    private Environment environment;

    @Value("${spring.rabbitmq.operationlog.exchange.name}")
    private String operationlogExchange;

    @Value("${spring.rabbitmq.operationlog.routing-key}")
    private String operationlogRoutingKey;

    // web拦截
    @Pointcut("within(com.wust.smartcity.autotask.server.core.api.web..*)")
    private void web(){}

    //环绕通知
    @Before("web()")
    public void before(JoinPoint jp) throws Throwable {
        super.before(jp);
    }

    @Override
    protected String getServerName() {
        return environment.getProperty("spring.application.name");
    }

    @Override
    protected void insert(BlOperationLog sysOperationLog) {
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(sysOperationLog);
        producer4routingKey.send(operationlogExchange,operationlogRoutingKey,jsonObject);
    }
}
